package com.bootdo.system.service.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.bootdo.system.dao.DyMemberDao;
import com.bootdo.system.domain.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.bootdo.app.config.WXConfig;
import com.bootdo.app.dto.wx.TemplateMsgRequest;
import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.HttpUtil;
import com.bootdo.common.utils.Query;
import com.bootdo.system.domain.TbWxuserExample.Criteria;
import com.bootdo.system.mapper.TbJcLogMapper;
import com.bootdo.system.mapper.TbOrderMapper;
import com.bootdo.system.mapper.TbQdtxMapper;
import com.bootdo.system.mapper.TbViporderMapper;
import com.bootdo.system.mapper.TbWithdrawalMapper;
import com.bootdo.system.mapper.TbWxUserMeetVipMapper;
import com.bootdo.system.mapper.TbWxuserMapper;
import com.bootdo.system.service.WxUserService;


@Service
public class WxUserServiceImpl implements WxUserService{
	
	@Autowired
    private TbWxuserMapper wm;
	
	@Autowired
    private TbJcLogMapper jm;
	
	@Autowired
    private TbWxUserMeetVipMapper um;
	
	@Autowired
    private TbWithdrawalMapper tm;
	
	@Autowired
    private TbOrderMapper om;
	
	@Autowired
    private TbViporderMapper vm;
	
	@Autowired
    private TbQdtxMapper qm;

	@Autowired
    private DyMemberDao dy;

	@Override
	public List<TbWxuser> list(Map<String, Object> map) {
		return wm.list(map);
	}

	@Override
	public int insert2update(TbWxuser table) {
		int res=0;
		if(table != null) {
			Integer id = table.getId();
			if(id != null) {
				res=wm.updateByPrimaryKeySelective(table);
			}else {
				table.setSubscribeTime( DateUtils.format(new Date()));
				res=wm.insertSelective(table);
			}
		}
		return res;
	}

	@Override
	public int count(Map<String, Object> map) {
		return wm.count(map);
	}

	@Override
	public TbWxuser getById(Integer id) {
		return wm.selectByPrimaryKey(id);
	}

	@Override
	public TbWxuser selectByWxOpenId(String openId) {
		TbWxuserExample example = new TbWxuserExample();
		TbWxuserExample.Criteria criteria = example.createCriteria();
		criteria.andOpenidXcxEqualTo(openId);
//		criteria.andOpenidEqualTo(openId);
		List<TbWxuser> tbWxusers = wm.selectByExample(example);
		if(tbWxusers == null || tbWxusers.isEmpty()){
			return null;
		}
		return tbWxusers.get(0);
	}

	@Override
	public int batchremove(Integer[] wxIds) {
		int res=0;
		if(wxIds.length > 0) {
			for (int i = 0; i < wxIds.length; i++) {
				res=wm.deleteByPrimaryKey(wxIds[i]);
			}
		}
		return res;
	}

	@Override
	public int remove(Integer id) {
		return wm.deleteByPrimaryKey(id);
	}

	@Override
	public List<TbWxuser> getUserByOpenId(String openId) {
		List<TbWxuser> res=new ArrayList<TbWxuser>();
		List<TbWxuser> userByOpenId = wm.getUserByOpenId(openId);
		if(userByOpenId != null && userByOpenId.size() > 0) {
			for (TbWxuser tbWxuser : userByOpenId) {
				Integer id = tbWxuser.getId();
				res.add(tbWxuser);
				List<TbWxuser> userByOpenId2 = wm.getUserByOpenId(id.toString());
				for (TbWxuser tbWxuser2 : userByOpenId2) {
					res.add(tbWxuser2);
				}
			}
		}
		return res;
	}

	@Override
	public TbWxuser getUserById(String openId) {
		TbWxuserExample tbWxuserExample = new TbWxuserExample();
		Criteria createCriteria = tbWxuserExample.createCriteria();
		if(StringUtils.isNoneBlank(openId)) {
			//修改为小程序的openid
			createCriteria.andOpenidXcxEqualTo(openId);
			List<TbWxuser> selectByExample = wm.selectByExample(tbWxuserExample);
			if(selectByExample.size() > 0) {
				return selectByExample.get(0);
			}
		}
		return null;
	}

	@Override
	public TbWxuser selectUserInfoByUnionid(String string) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int insertJc(TbJcLog table) {
		table.setCjsj(DateUtils.format(new Date()));
		return jm.insertSelective(table);
	}

	@Override
	public List<Map<String, Object>> listJc(Map<String, Object> map) {
		return jm.listJc(map);
	}

	@Override
	public int countJc(Map<String, Object> map) {
		return jm.countJc(map);
	}

	@Override
	public List<Map<String, Object>> listJc2Dept(Map<String, Object> map) {
		return jm.listJc2Dept(map);
	}

	@Override
	public int countJc2Dept(Map<String, Object> map) {
		return jm.countJc2Dept(map);
	}

	@Override
	public List<Map<String, Object>> listJc2Dept2(Map<String, Object> params) {
		return jm.listJc2Dept2(params);
	}

	@Override
	public int countJc2Dept2(Map<String, Object> params) {
		return jm.countJc2Dept2(params);
	}

	@Override
	public List<Map<String, Object>> listJc2(Map<String, Object> params) {
		return jm.listJc2(params);
	}

	@Override
	public int countJc2(Map<String, Object> params) {
		return jm.countJc2(params);
	}

	@Override
	public List<TbWxUserMeetVip> listLqhy(Map<String, Object> map) {
		return um.listLqhy(map);
	}

	@Override
	public int countLqhy(Map<String, Object> map) {
		return um.countLqhy(map);
	}

	@Override
	public int updateLqhy(TbWxUserMeetVip table) {
		return um.updateByPrimaryKeySelective(table);
	}

	@Override
	public TbWxuser getUserByOpenId2(String openId) {
		List<TbWxuser> userByOpenId = wm.getUserByOpenId2(openId);
		if(userByOpenId != null && userByOpenId.size() > 0) {
			TbWxuser tbWxuser = userByOpenId.get(0);
			TbQdtxExample tbQdtxExample = new TbQdtxExample();
			com.bootdo.system.domain.TbQdtxExample.Criteria createCriteria = tbQdtxExample.createCriteria();
			createCriteria.andWxIdEqualTo(tbWxuser.getId());
			List<TbQdtx> selectByExample = qm.selectByExample(tbQdtxExample);
			if(selectByExample != null && selectByExample.size() > 0) {
				TbQdtx tbQdtx = selectByExample.get(0);
				tbWxuser.setQdti(tbQdtx.getSftx());
			}else {
				tbWxuser.setQdti(0);
			}
			Integer wxuserId = tbWxuser.getWxuserId();
			tbWxuser.setTjr(null);
			if(wxuserId != null && wxuserId > 1) {
				TbWxuser selectByPrimaryKey = wm.selectByPrimaryKey(wxuserId);
				if(selectByPrimaryKey != null) {
					tbWxuser.setTjr(selectByPrimaryKey.getNickname());
				}
			}
			String num = tm.getNum(tbWxuser.getId());
			String num2=om.getNum(tbWxuser.getOpenidXcx());
			String jrsy = om.getProfit2Order3(tbWxuser.getOpenidXcx(), DateUtils.format2(new Date()));
			String zrsy = om.getProfit2Order3(tbWxuser.getOpenidXcx(), DateUtils.format5());
			tbWxuser.setJrsy(getDouble2String(jrsy)+"");
			tbWxuser.setZrsy(getDouble2String(zrsy)+"");
			String balance = tbWxuser.getBalance();
			tbWxuser.setDtx(getDouble2String(num2)+"");
			tbWxuser.setBalance(getDouble2String(balance)+"");
			if(num == null) {
				tbWxuser.setLjsy("0");
			}else {
				tbWxuser.setLjsy(getDouble2String(num)+"");
			}
			return tbWxuser;
		}
		return null;
	}
	
	private Double getDouble2String(String num) {
		if(StringUtils.isNoneBlank(num)) {
			Double cny = Double.parseDouble(num);
			DecimalFormat df = new DecimalFormat("0.00"); 
			String CNY = df.format(cny);
			Double cny1 = Double.parseDouble(CNY);
			return cny1;
		}
		return 0.00;
	}

	@Override
	public List<Map<String, Object>> listJc2Log(Integer userId) {
		return jm.listJc2Log(userId);
	}

	@Override
	public int getNum(Integer intValue, String format2) {
		return wm.getNum(intValue,format2);
	}

	@Override
	public TbWxuser getUserByUnioId(String unioId) {
		TbWxuserExample example = new TbWxuserExample();
		TbWxuserExample.Criteria criteria = example.createCriteria();
		criteria.andWxUnionIdEqualTo(unioId);
//		criteria.andOpenidEqualTo(openId);
		List<TbWxuser> tbWxusers = wm.selectByExample(example);
		if(tbWxusers == null || tbWxusers.isEmpty()){
			return null;
		}
		return tbWxusers.get(0);
	}
//	 private final String SEND_TEMPLATE_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send";

	@Override
	public int insertBatch(List<TbWxuser> newList) {
		// TODO Auto-generated method stub
		return wm.insertBatch(newList);
	}

	@Override
	public int selectLeaderNum(Integer leaderId,String appId) {
		if(leaderId ==null){
			return 0;
		}
		int num = wm.selectLeaderNum(leaderId);
        List<DyMember> dyMembers = dy.selectMemberList(appId, leaderId);
        DyMember dyMember = dyMembers.get(0);
        Integer member = dyMember.getMember();
        Integer glodMember = dyMember.getGlodMember();
        Integer jewelMemeber = dyMember.getJewelMemeber();
        Integer heightMember = dyMember.getHeightMember();
        Integer endMember = dyMember.getEndMember();
        TbWxuser wxuser= new TbWxuser();
        wxuser.setId(leaderId);
        if(num >=member && num < glodMember){
            wxuser.setUserType(1);
        }else if(num >= jewelMemeber && num < heightMember){
            wxuser.setUserType(2);
        }else if (num >= heightMember && num < endMember){
            wxuser.setUserType(3);
        }else if(num >= endMember){
            wxuser.setUserType(4);
        }
        wm.updateByPrimaryKeySelective(wxuser);
        return 0;
	}

	@Override
	public TbWxuser selectUserInfoByGzh(String string) {
		TbWxuserExample example = new TbWxuserExample();
		TbWxuserExample.Criteria criteria = example.createCriteria();
		if(string == null) {
			return null;
		}
		criteria.andGzhOpenIdEqualTo(string);
		List<TbWxuser> tbWxusers = wm.selectByExample(example);
		if(tbWxusers == null || tbWxusers.isEmpty()){
			return null;
		}
		return tbWxusers.get(0);
	}

	@Override
	public TbWxuser getUserById2(String openId) {
		TbWxuserExample tbWxuserExample = new TbWxuserExample();
		Criteria createCriteria = tbWxuserExample.createCriteria();
		if(StringUtils.isNoneBlank(openId)) {
			//修改为小程序的openid
			createCriteria.andGzhOpenIdEqualTo(openId);
			List<TbWxuser> selectByExample = wm.selectByExample(tbWxuserExample);
			if(selectByExample.size() > 0) {
				return selectByExample.get(0);
			}
		}
		return null;
	}

	@Override
	public Map<String, Object> getUserByOpenId5(String openId,String openId2) {
		Map<String,Object> map=new HashMap<String, Object>();
		List<TbWxuser> res=new ArrayList<TbWxuser>();
		List<TbWxuser> ejcy=new ArrayList<TbWxuser>();
		List<TbWxuser> userByOpenId = wm.getUserByOpenId(openId);
		map.put("yjcy", userByOpenId);
		int tuddNum=0;
		int jrddNum=0;
		
		int jrdd8 = vm.getDirect2VipOrderNum2(openId2,DateUtils.format4(new Date()));
		jrddNum=jrddNum+jrdd8;
		
		int direct2OrderNum8 = om.getDirect2OrderNum2(openId2,DateUtils.format2(new Date()));
		jrddNum=jrddNum+direct2OrderNum8;
		
		int tudd8 = vm.getDirect2VipOrderNum2(openId2,null);
		tuddNum=tuddNum+tudd8;
		
		int direct2OrderNum9 = om.getDirect2OrderNum2(openId2,null);
		tuddNum=tuddNum+direct2OrderNum9;
		if(userByOpenId != null && userByOpenId.size() > 0) {
			for (TbWxuser tbWxuser : userByOpenId) {
				String openidXcx = tbWxuser.getOpenidXcx();
				Integer id = tbWxuser.getId();
				if(openidXcx != null) {
					int jrdd = vm.getDirect2VipOrderNum2(openidXcx,DateUtils.format4(new Date()));
					jrddNum=jrddNum+jrdd;
					
					int direct2OrderNum2 = om.getDirect2OrderNum2(openidXcx,DateUtils.format2(new Date()));
					jrddNum=jrddNum+direct2OrderNum2;
					
					int tudd = vm.getDirect2VipOrderNum2(openidXcx,null);
					tuddNum=tuddNum+tudd;
					
					int direct2OrderNum3 = om.getDirect2OrderNum2(openidXcx,null);
					tuddNum=tuddNum+direct2OrderNum3;
					
					tbWxuser.setDdsl(tudd+direct2OrderNum3);
					int num=getTuNum(tbWxuser.getId());
					tbWxuser.setTdsl(num);
				}
				res.add(tbWxuser);
				List<TbWxuser> userByOpenId2 = wm.getUserByOpenId(id.toString());
				for (TbWxuser tbWxuser2 : userByOpenId2) {
					String openidXcx2 = tbWxuser2.getOpenidXcx();
					if(openidXcx2 != null) {
						int jrdd2 = vm.getDirect2VipOrderNum2(openidXcx2,DateUtils.format4(new Date()));
						jrddNum=jrddNum+jrdd2;
						
						int direct2OrderNum4 = om.getDirect2OrderNum2(openidXcx2,DateUtils.format2(new Date()));
						jrddNum=jrddNum+direct2OrderNum4;
						
						
						int tudd2 = vm.getDirect2VipOrderNum2(openidXcx2,null);
						tuddNum=tuddNum+tudd2;
						
						int direct2OrderNum5 = om.getDirect2OrderNum2(openidXcx2,null);
						tuddNum=tuddNum+direct2OrderNum5;
						
						tbWxuser2.setDdsl(tudd2+direct2OrderNum5);
						int num2=getTuNum(tbWxuser2.getId());
						tbWxuser2.setTdsl(num2);
					}
				
					res.add(tbWxuser2);
					ejcy.add(tbWxuser2);
				}
			}
		}
		map.put("ejcy", ejcy);
		map.put("sycy", res);
		map.put("jrdd", jrddNum);
		map.put("tudd", tuddNum);
		return map;
	}

	private int getTuNum(Integer id) {
		int num=0;
		List<TbWxuser> userByOpenId = wm.getUserByOpenId(id.toString());
		int size = userByOpenId.size();
		num=num+size;
		if(userByOpenId != null && userByOpenId.size() > 0) {
			for (TbWxuser tbWxuser : userByOpenId) {
				Integer id2 = tbWxuser.getId();
				List<TbWxuser> userByOpenId2 = wm.getUserByOpenId(id2.toString());
				int size2 = userByOpenId2.size();
				num=num+size2;
			}
		}
		return num;
	}

	@Override
	public int updateQdtx(Integer wxId, int status) {
		int res=0;
		TbQdtxExample tbQdtxExample = new TbQdtxExample();
		com.bootdo.system.domain.TbQdtxExample.Criteria createCriteria = tbQdtxExample.createCriteria();
		createCriteria.andWxIdEqualTo(wxId);
		List<TbQdtx> selectByExample = qm.selectByExample(tbQdtxExample);
		if(selectByExample != null && selectByExample.size() > 0) {
			if(selectByExample.size() > 1) {
				for (TbQdtx tbQdtx : selectByExample) {
					qm.deleteByPrimaryKey(tbQdtx.getId());
				}
				TbQdtx record=new TbQdtx();
				record.setWxId(wxId);
				record.setSftx(status);
				res=qm.insertSelective(record);
			}else {
				TbQdtx tbQdtx = selectByExample.get(0);
				tbQdtx.setSftx(status);
				res=qm.updateByPrimaryKeySelective(tbQdtx);
			}
		}else {
			TbQdtx record=new TbQdtx();
			record.setWxId(wxId);
			record.setSftx(status);
			res=qm.insertSelective(record);
		}
		return res;
	}

	@Override
	public List<TbQdtx> getQdtxByStatus(int sfts) {
		TbQdtxExample tbQdtxExample = new TbQdtxExample();
		com.bootdo.system.domain.TbQdtxExample.Criteria createCriteria = tbQdtxExample.createCriteria();
		createCriteria.andSftxEqualTo(sfts);
		return qm.selectByExample(tbQdtxExample);
	}

//	@Override
//	public String sendTemplateMsg2(String token, TemplateMsgRequest templateMsg) {
//		TreeMap<String, String> treeMap = new TreeMap<String, String>();
//		   treeMap.put("access_token", token);
//		   String retInfo = HttpUtil.doPost(SEND_TEMPLATE_MESSAGE_URL, treeMap, JSON.toJSONString(templateMsg).toString());
//		   return retInfo;
//	}

}
